// LazyMan('Tony');
// Hi I am Tony

// LazyMan('Tony').sleep(10).eat('lunch');
// Hi I am Tony
// 等待了10秒...
// I am eating lunch

// LazyMan('Tony').eat('lunch').sleep(10).eat('dinner');
// Hi I am Tony
// I am eating lunch
// 等待了10秒...
// I am eating diner

LazyMan('Tony').eat('lunch').eat('dinner').sleepFirst(3).sleep(5).eat('junk food');
// Hi I am Tony
// 等待了3秒...
// I am eating lunch
// I am eating dinner
// 等待了5秒...
// I am eating junk food

function LazyMan(name) {
  class ManClass {
    constructor(name) {
      this.task = [];
      console.log(`Hi I am ${name}`);
      Promise.resolve().then(() => this.next())
      return this;
    }
    eat(name) {
      const fn1 = res => {
        console.log(`I am eating ${name}`);
        this.next();
      }
      this.task.push(fn1);
      return this;
    }
    sleepFirst(time) {
      const fn2 = () => {
        setTimeout(() => {
          console.log(`等待了${time}秒`);
          this.next()
        }, time * 1000)
      }
      this.task.unshift(fn2);
      return this;
    }
    sleep(time) {
      const fn3 = () => {
        setTimeout(() => {
          console.log(`等待了${time}秒`);
          this.next();
        }, time * 1000)
      }
      this.task.push(fn3);
      return this;
    }
    next() {
      const fn = this.task.shift()
      fn && fn();
    }
  }

  return new ManClass(name)
}